<?php

namespace App\Http\Controllers\Wechat;

use App\Models\Build\Star\Ranks;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Wechat\BaseController;
use App\Models\BuildPrice;
use App\Models\Areas;
use App\Models\BuildType;
use App\Models\Buildings;
use Illuminate\Support\Facades\Input;
use Event;
use  App\Libraries\classs\PhpRedis;
use App\Models\Build\Detailed as Detailed;
use Symfony\Component\VarDumper\Cloner\Data;
use App\Models\User\Build\Logs as BuildLogs;

class IndexController extends BaseController
{
    public function init(){

    }
    
    public function index(){
        $data =array();
         // 获取总用户人数
        $data['user_count']=PhpRedis::getInstance()->getRedisUserCount();
        //总星数
        $data['build_star_sum']=PhpRedis::getInstance()->getRedisBuildStarSum();
        //总人气
        $data['build_count_sum']=PhpRedis::getInstance()->getRedisBuildCountSum();

        $data['build_price']=BuildPrice::get();
        $data['build_areas']=Areas::get();
        $data['build_type'] = BuildType::get();

        $data['status']=$this->today();

       return view('wechat.index.index',array('data'=>$data));
    }

    public function load(Request $request){
        $data = array('success'=>true);
        if($request->ajax()){

            $wordkeys = Input::get('wordkeys');
            if($wordkeys){
                $where =array(array('name','like','%'.$wordkeys.'%'));
                $build = Buildings::with('attach')->where($where)->orderByRaw('RAND()')->paginate($this->_page_count);
                $data['build']['search']=true;
            }else{
                $build = Buildings::with('attach')->orderByRaw('RAND()')->paginate(30);
                $data['build']['search']=false;
            }
            $data['build']=$build;
        }else{
            $data['success']=false;
        }
       return response()->json($data);
    }

    //搜索
    public function loadSearch(Request $request){
        $data = array('success'=>false);
        if($request->ajax()){
            $wordkeys = Input::get('wordkeys');
            if($wordkeys){
                $where =array(array('name','like','%'.$wordkeys.'%'));
//                $where['area_id'] =1;
                $count = Buildings::where($where)->count();
                if($count){
                    $build = Buildings::with('attach')->where($where)->orderBy('id','desc')->paginate($this->_page_count);
                    $data['build']=$build;
                    $data['success']=true;
                }
            }else{
                $count = Buildings::count();
                if($count){
                    $build = Buildings::with('attach')->orderBy('id','desc')->paginate($this->_page_count);
                    $data['build']=$build;
                    $data['success']=true;
                }
            }
        }
        return response()->json($data);
    }

    public function voteLog(Request $request){
        $data = array('success'=>false);
        if($request->ajax()){
            $vole_log =PhpRedis::getInstance()->getVoteLog();
            if($vole_log){
                $data['volelog']=$vole_log;
                $data['success']=true;
            }
        }
        return response()->json($data);
    }

    public function buildDetailed(Request $request){
            $b_id = $request->get('id');
            $build = Buildings::find($b_id);
            $build_detail = Detailed::where('build_id','=',$b_id)->first();
            $BuildLogs = BuildLogs::where('x_uid','=',$this->_uid)->where('build_id','=',$b_id)->first();
            $build_rank = Ranks::where('build_id','=',$b_id)->select('rank','stas')->first();
            if(!$BuildLogs){
                $log_data =array(
                    'x_uid'=>$this->_uid,
                    'build_id'=>$b_id,
                    'attach_id'=>$build->attach_id,
                );
                BuildLogs::create($log_data);
            }else{
                $BuildLogs->updated_at= format(time());
                $BuildLogs->save();
            }
            $build->count = $build->count+1;
            $build->update();
            return view('wechat.index.build.detailed',array('data'=>$build,'build_detail'=>$build_detail,'build_rank'=>$build_rank));
    }
}
